package com.lsh.action.system;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.lsh.action.AbstractManagerAction;
import com.lsh.model.TsysUser;
import com.lsh.service.MenuService;
import com.lsh.service.TsysUserService;

@ParentPackage("json-default")
@Namespace(value = "/test")
@Results( { @Result(name = "success", location = "/system/index.jsp"),
		@Result(name = "changePassword",type="json"),
		@Result(name = "login",location="/system/login.jsp"),
		@Result(name="logout" ,type="redirect",location="/system/login.jsp"),
		@Result(name = "error", location = "/system/error.jsp")}) 
public class LoginAction extends AbstractManagerAction {
	public String right = "common";
	private String userName;
	private String password;
	private String newpassword;
	@Autowired
	private MenuService menuService;
	@Autowired
	private TsysUserService userService;

	@Action("login")
	public String login(){
		TsysUser user = new TsysUser();
		if(userName!=null && !userName.equals("")){
			user.setPassword(password);
			user.setUsername(userName);
			TsysUser tsysUser =menuService.getUser(user);
			if(null == tsysUser ){
				message="用户名或者密码错误";
				return "login";
			}
			List menuList = userService.getMenuListByUser(tsysUser);
			HashSet rightSet = userService.getRightSetByUser(tsysUser);
			setSession("rights", rightSet);
			setSession("menuList",menuList);
			setSession("currentUser",tsysUser);
			return SUCCESS; 
		}else{
			return "login";
		}
	}
	
	@Action("logout")
	public String logout(){
		Enumeration enums = request.getSession().getAttributeNames();
		while(enums.hasMoreElements()){
			String name = (String) enums.nextElement();
			System.out.println("session name:"+name);
			request.getSession().removeAttribute(name);
		}
		return "logout"; 
	}
	
	@Action("changePassword")
	public String  changePassword() throws IOException {
		TsysUser user = (TsysUser) get("currentUser");
		if(newpassword!=null && !newpassword.equals("")){
			if(!user.getPassword().equals(password)){
				message="原密码错误，无法更改";
				return "changePassword";
			}
			user.setPassword(newpassword);
			userService.update(user);
			message="密码修改成功";
		}else{
			message="新密码不能为空";
		}
		response.setContentType("text/plain; charset=UTF-8");
		response.setHeader("Content-Disposition", "inline");
		PrintWriter writer = response.getWriter();
		writer.print(message);
		System.out.println(message);
		writer.flush();  
		writer.close();
		return "changePassword";
	}
	
	public String defaultAction() {
		return SUCCESS;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}
}
